package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;


    @GetMapping("/query")
    public Object query(TravelQueryObject qo) {
        qo.setState(2); // 只查看已发布
        Page<Travel> travels = travelService.query(qo);
        return JsonResult.success(travels);
    }

    @RequireLogin
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {
        travelService.saveOrUpdate(travel, userInfo);
        return JsonResult.success(travel.getId());
    }

    @GetMapping("/comments")
    public Object comments(String travelId) {
        List<TravelComment> comments = travelService.comments(travelId);
        return JsonResult.success(comments);
    }

    @GetMapping("/detail")
    public Object detail(String id) {
        System.out.println(id);
        Travel detail = travelService.get(id);
        return JsonResult.success(detail);
    }

    @GetMapping("/getTravelsByTop3")
    public Object getTravelsByTop3(String destId) {
        List<Travel> travels = travelService.getTravelsByTop3(destId);
        return JsonResult.success(travels);
    }


    @RequireLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment, @UserParam UserInfo userInfo) {
        travelService.commentAdd(travelComment, userInfo);
        return JsonResult.success();
    }











    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }

}
